/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VistaOrden.java
 *
 * Created on 08/04/2011, 01:01:38 AM
 */

package vistas;

import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.util.LinkedList;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author usuario
 */
public class VistaOrden extends javax.swing.JFrame {

    private controladores.ControladorOrden control;
    private DefaultTableModel modeloTabla;
    private Vector<String> fila;
    /** Creates new form VistaOrden */
    public VistaOrden() {
        initComponents();
        control= new controladores.ControladorOrden(this);
        modeloTabla = new DefaultTableModel();
        fila = new Vector<String>();
        tblProdcuto.setModel(modeloTabla);
        fila.add("Producto");
        fila.addElement("Precio");
        fila.add("Cantidad");
        fila.add("Sub-Total");
        modeloTabla.setColumnIdentifiers(fila);
        txtTotal.setEditable(false);
        addAction();
    }
    
    public void mostrarMensaje(String mensaje){
        JOptionPane.showMessageDialog(new JFrame(),mensaje,"Cari Burger",JOptionPane.INFORMATION_MESSAGE);

    	
    }
    
    public void windowClosing(WindowEvent e){
		setVisible(false);
	}


     public void addListener(ActionListener escuchador){
        btnCancelar.addActionListener(escuchador);
        btnMas.addActionListener(escuchador);
        btnMenos.addActionListener(escuchador);
        btnRegistrar.addActionListener(escuchador);
        btnSalir.addActionListener(escuchador);
    }

     public void addAction(){
        btnCancelar.setActionCommand("Cancelar");
        btnMas.setActionCommand("Mas");
        btnMenos.setActionCommand("Menos");
        btnRegistrar.setActionCommand("Registrar");
        btnSalir.setActionCommand("Salir");
    }

    public void menos(){
        int i = tblProdcuto.getSelectedRow();
        if(i != -1){
            float t = Float.parseFloat(txtTotal.getText())-Float.parseFloat((String) modeloTabla.getValueAt(i, 3));
            modeloTabla.removeRow(i);
            txtTotal.setText(""+t);
        }

        else
            JOptionPane.showMessageDialog(new JFrame(),"Debe seleccionar el prodcuto que desea quitar","Cari Burguer",JOptionPane.ERROR_MESSAGE);
    }




     
    

    public LinkedList<JTextField> getTextos() {
        LinkedList<JTextField> v = new LinkedList<JTextField>();
        v.add(txtCodigo);
        v.add(txtCedula);

        return v;
    }

    public boolean actualizar(){
    	float total;
    	if(!txtTotal.getText().isEmpty())
            total = Float.parseFloat(txtTotal.getText());
        else
            total=0;
    	for (int i = 0; i < modeloTabla.getRowCount(); i++)
            if(modeloTabla.getValueAt(i, 0).equals(cmbProducto.getItemAt(cmbProducto.getSelectedIndex()))){
                int c = getCantidadProdcuto();
                double sub = Double.parseDouble(""+modeloTabla.getValueAt(i, 1));
                modeloTabla.setValueAt(""+c, i, 2);
                modeloTabla.setValueAt(""+(sub*c), i, 3);
                total-=sub;
                System.out.println(total);
                total+=(sub*c);
                txtTotal.setText(""+total);
                return true;
            }
        return false;
    }


    public void agregarFila(String[] v, float precio){
        float total;
        if(!txtTotal.getText().isEmpty())
            total = Float.parseFloat(txtTotal.getText());
        else
            total=0;
        if(!actualizar()){
            int c = getCantidadProdcuto();
            fila= new Vector<String>();
            fila.add(v[0]);
            fila.add(""+precio);
            fila.add(""+c);
            float sub = (precio*c);
            fila.add("" + sub);
            total+=sub;
            txtTotal.setText(""+total);
            modeloTabla.addRow(fila);
        }
    }

    public JTextField getTxtCedula() {
        return txtCedula;
    }

    public JTextField getTxtCodigo() {
        return txtCodigo;
    }

    public JTextField getTxtTotal() {
        return txtTotal;
    }

    public LinkedList<String[]> getInfoTabla() {
        LinkedList<String []> v = new LinkedList<String[]>();
        for (int i = 0; i < modeloTabla.getRowCount(); i++){
            String [] s = new String [] {""+modeloTabla.getValueAt(i, 0),""+modeloTabla.getValueAt(i, 2)};
            v.add(s);
         }
         return v;
    }


      public boolean hayVacios(){
        for (int i = 0; i < getTextos().size(); i++) {
            JTextField texto = getTextos().get(i);
            if(texto.getText().equals("")){
                JOptionPane.showMessageDialog(new JFrame(),"Campos vacios","Cari Burguer",JOptionPane.INFORMATION_MESSAGE);
                return false;
            }
        }
        return true;
    }

    public boolean validoNumeros(){
        try{
            if(Double.parseDouble(txtCodigo.getText())<=0){
                txtCodigo.setText("");
                JOptionPane.showMessageDialog(new JFrame(),"El codigo es invalido","Cari Burguer",JOptionPane.WARNING_MESSAGE);
                return false;
           }
       }catch(NumberFormatException ex){
           JOptionPane.showMessageDialog(new JFrame(),"Ingreso letras en lugar de numeros","Cari Burguer",JOptionPane.WARNING_MESSAGE);
           return false;
       }
    return true;
    }


    public void calcularTotal(){
        float total =0;
        for (int i = 0; i < modeloTabla.getRowCount(); i++)
            total+=Float.parseFloat(""+modeloTabla.getValueAt(i, 3));
        txtTotal.setText(""+total);
    }

      public boolean validarCantidad(){
        if(getCantidadProdcuto()<=0){
                JOptionPane.showMessageDialog(new JFrame(),"Cantidad no permitida","Cari Burguer",JOptionPane.INFORMATION_MESSAGE);
                return false;
            }

        return true;
    }


     public void limpiar(){
         for (JTextField texto : getTextos()) {
            texto.setText("");
        }
        cmbProducto.setSelectedIndex(0);
        modeloTabla.setRowCount(0);
        spnCantidad.setValue(0);
        txtTotal.setText("");
     }

    public void cargarComboProductos(Vector<String> descripciones){
        cmbProducto.removeAllItems();
        for (int i = 0; i < descripciones.size(); i++) {
            cmbProducto.addItem(descripciones.elementAt(i));

        }
    }

    

  
    public String getTextoCmbProducto() {
        return (String) cmbProducto.getItemAt(cmbProducto.getSelectedIndex());
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtTotal = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        txtCedula = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblProdcuto = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        btnRegistrar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        spnCantidad = new javax.swing.JSpinner();
        btnMas = new javax.swing.JButton();
        btnMenos = new javax.swing.JButton();
        cmbProducto = new javax.swing.JComboBox();
        jLabel11 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Registro de  Ordenes");
        getContentPane().setLayout(null);

        txtTotal.setFont(new java.awt.Font("Arial", 0, 13));
        getContentPane().add(txtTotal);
        txtTotal.setBounds(710, 390, 110, 30);

        jLabel5.setFont(new java.awt.Font("Arial", 1, 15)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(244, 236, 236));
        jLabel5.setText("Total:");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(650, 390, 55, 30);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setLayout(null);

        jLabel6.setFont(new java.awt.Font("Monospaced", 1, 15));
        jLabel6.setText("Codigo:");
        jPanel1.add(jLabel6);
        jLabel6.setBounds(20, 10, 100, 30);

        txtCodigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCodigoActionPerformed(evt);
            }
        });
        jPanel1.add(txtCodigo);
        txtCodigo.setBounds(100, 10, 200, 30);

        txtCedula.setFont(new java.awt.Font("Arial", 0, 13));
        jPanel1.add(txtCedula);
        txtCedula.setBounds(100, 50, 200, 30);

        jLabel3.setFont(new java.awt.Font("Monospaced", 1, 15));
        jLabel3.setText("Cedula:");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(20, 50, 100, 32);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(80, 60, 340, 100);

        tblProdcuto.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblProdcuto);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(500, 220, 380, 160);

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel3.setLayout(null);

        btnRegistrar.setFont(new java.awt.Font("Monospaced", 1, 15));
        btnRegistrar.setText("Registrar");
        btnRegistrar.setPreferredSize(new java.awt.Dimension(80, 30));
        jPanel3.add(btnRegistrar);
        btnRegistrar.setBounds(20, 20, 120, 30);

        btnCancelar.setFont(new java.awt.Font("Monospaced", 1, 15));
        btnCancelar.setText("Cancelar");
        btnCancelar.setPreferredSize(new java.awt.Dimension(80, 30));
        jPanel3.add(btnCancelar);
        btnCancelar.setBounds(150, 20, 120, 30);

        btnSalir.setFont(new java.awt.Font("Monospaced", 1, 15));
        btnSalir.setText("Salir");
        jPanel3.add(btnSalir);
        btnSalir.setBounds(280, 20, 120, 30);

        getContentPane().add(jPanel3);
        jPanel3.setBounds(40, 210, 420, 70);

        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel4.setLayout(null);

        jLabel9.setFont(new java.awt.Font("Monospaced", 1, 15));
        jLabel9.setText("Seleccione");
        jPanel4.add(jLabel9);
        jLabel9.setBounds(20, 20, 240, 30);

        jLabel10.setFont(new java.awt.Font("Monospaced", 1, 15));
        jLabel10.setText("Cantidad");
        jPanel4.add(jLabel10);
        jLabel10.setBounds(20, 70, 110, 30);
        jPanel4.add(spnCantidad);
        spnCantidad.setBounds(130, 68, 36, 30);

        btnMas.setFont(new java.awt.Font("Monospaced", 1, 15));
        btnMas.setText("+");
        jPanel4.add(btnMas);
        btnMas.setBounds(310, 20, 60, 30);

        btnMenos.setFont(new java.awt.Font("Monospaced", 1, 15));
        btnMenos.setText("-");
        jPanel4.add(btnMenos);
        btnMenos.setBounds(310, 60, 60, 30);

        jPanel4.add(cmbProducto);
        cmbProducto.setBounds(130, 20, 170, 30);

        getContentPane().add(jPanel4);
        jPanel4.setBounds(500, 100, 380, 110);

        jLabel11.setFont(new java.awt.Font("Arial", 1, 15)); // NOI18N
        jLabel11.setForeground(new java.awt.Color(244, 236, 236));
        jLabel11.setText("Productos");
        getContentPane().add(jLabel11);
        jLabel11.setBounds(620, 70, 180, 22);

        jLabel4.setFont(new java.awt.Font("Arial", 1, 15)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(244, 236, 236));
        jLabel4.setText("Registro de Ordenes");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(140, 30, 230, 22);

        jLabel1.setBackground(new java.awt.Color(36, 66, 215));
        jLabel1.setOpaque(true);
        getContentPane().add(jLabel1);
        jLabel1.setBounds(0, 0, 920, 440);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-921)/2, (screenSize.height-466)/2, 921, 466);
    }// </editor-fold>//GEN-END:initComponents

    private void txtCodigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCodigoActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtCodigoActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VistaOrden().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnMas;
    private javax.swing.JButton btnMenos;
    private javax.swing.JButton btnRegistrar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JComboBox cmbProducto;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSpinner spnCantidad;
    private javax.swing.JTable tblProdcuto;
    private javax.swing.JTextField txtCedula;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtTotal;
    // End of variables declaration//GEN-END:variables

    public String [] getRegistro() {
          String [] s = new String [] {""+cmbProducto.getItemAt(cmbProducto.getSelectedIndex()),""+getCantidadProdcuto()};

         return s;
    }

    public Integer getCantidadProdcuto() {
        return (Integer) spnCantidad.getValue();
    }



}
